﻿using System;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.AI.Node
{
    [Category("Ggame/Liberty")]
    public class NpcTriggerFireRPG : AIBaseNode
    {
        // 朝地面开火的最小距离
        public BBParameter<float> fireGroundDistance = 3f;

        protected override void OnExecute()
        {
            // 检测自身状态
            AnimationAgent animationAgent = _entity.viewer.animationAgent;
            if (!animationAgent.CanDoNextAnimation())
            {
                EndAction(false);
                return;
            }

            BattleAgent battleAgent = _entity.battleAgent;
            AIEntity currentLockTarget = battleAgent.currentLockTarget;
            {
                LibertyWeapon libertyWeapon = _entity.weapon;
                if (libertyWeapon != null)
                {
                    if (currentLockTarget && Vector3.Distance(battleAgent.transform.position, currentLockTarget.battleAgent.transform.position) >= fireGroundDistance.value)
                    {
                        // 目标有一定距离，朝目标脚下开火
                        Vector3 direction = (currentLockTarget.battleAgent.transform.position - libertyWeapon.FirePoint.position).normalized;
                        animationAgent.PlayRPGFire(libertyWeapon.FirePoint.position, direction);
                    }
                    else
                    {
                        // 否则直接朝前开火
                        animationAgent.PlayRPGFire(libertyWeapon.FirePoint.position, libertyWeapon.FirePoint.forward);
                    }
                }
                else
                {
                    EndAction(false);
                    return;
                }
            }

        }


        protected override void OnUpdate()
        {
            if (elapsedTime < 0.1f)
            {
                return;
            }
            
            AnimationAgent animationAgent = _entity.viewer.animationAgent;
            if (animationAgent.CanDoNextAnimation())
            {
                EndAction(true);
            }
        }

        
    }
}